package org.malred.hotelsystem.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import org.malred.hotelsystem.model.entity.HotelEntity;

import java.util.ArrayList;

public class HotelDao {
    private static final String TAG = "HotelDao";
    private static final String TB_NAME = "tb_hotel_room";

    /**
     * 添加
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean add(SQLiteDatabase db, HotelEntity entity) {
        ContentValues values = new ContentValues();
        values.put("h_id", entity.getId());
        values.put("h_title", entity.getTitle());
        values.put("h_desc", entity.getDescription());
        values.put("h_num", entity.getNumber());
        long i = db.insert(TB_NAME, null, values);
        return i > 0;
    }

    /**
     * 根据id删除
     *
     * @param db
     * @param id
     * @return
     */
    public static boolean del(SQLiteDatabase db, String id) {
        long i = db.delete(TB_NAME, "h_id=?", new String[]{id});
        return i > 0;
    }

    /**
     * 更新
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean upt(SQLiteDatabase db, HotelEntity entity) {
        ContentValues values = new ContentValues();
        if (entity.getTitle() != null) {
            values.put("h_title", entity.getTitle());
        }
        if (entity.getDescription() != null) {
            values.put("h_desc", entity.getDescription());
        }
        // 约定传入-1为不修改, 否则就修改
//        if (entity.getNumber() != -1) {
        values.put("h_num", entity.getNumber());
//        }
        int i = db.update(TB_NAME, values, "h_id=?", new String[]{entity.getId()});
        return i > 0;
    }

    /**
     * 查询所有
     *
     * @param db
     * @return
     */
    public static ArrayList<HotelEntity> getAll(SQLiteDatabase db) {
        ArrayList<HotelEntity> items = new ArrayList();
        Cursor c = db.query(TB_NAME, null, null,
                null, null, null, null);
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String title = c.getString(c.getColumnIndex("h_title"));
            @SuppressLint("Range") String desc = c.getString(c.getColumnIndex("h_desc"));
            @SuppressLint("Range") int num = c.getInt(c.getColumnIndex("h_num"));
            HotelEntity hotel = new HotelEntity();
            hotel.setId(id);
            hotel.setTitle(title);
            hotel.setDescription(desc);
            hotel.setNumber(num);
            items.add(hotel);
        }
        c.close();
        return items;
    }

    /**
     * 根据id查询
     *
     * @param db
     * @param idVal
     * @return
     */
    public static HotelEntity getById(SQLiteDatabase db, String idVal) {
        Cursor c = db.query(
                TB_NAME, null,
                "h_id=?", new String[]{idVal},
                null, null,
                null
        );
        HotelEntity hotel = new HotelEntity();
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("h_id"));
            @SuppressLint("Range") String title = c.getString(c.getColumnIndex("h_title"));
            @SuppressLint("Range") String desc = c.getString(c.getColumnIndex("h_desc"));
            @SuppressLint("Range") int num = c.getInt(c.getColumnIndex("h_num"));
            hotel.setId(id);
            hotel.setTitle(title);
            hotel.setDescription(desc);
            hotel.setNumber(num);
        }
        c.close();
        return hotel;
    }
}
